package jdk8;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class Java8Time {
    public static void main(String[] args) {
        // testLocalDateTime();
        testZoneDateTime();
    }

    /**
     * 时间和日期
     */
    public static void testLocalDateTime(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("当前时间： " + localDateTime);

        LocalDate localDate = localDateTime.toLocalDate();
        System.out.println("date1: " + localDate);

        Month month = localDateTime.getMonth();
        int day = localDateTime.getDayOfMonth();
        int seconds = localDateTime.getSecond();

        System.out.println("月： " + month + ", 日： " + day + ", 秒: " + seconds);

        // 修改当前时间的日期跟年份
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10).withYear(2012);
        System.out.println("date2 : " + localDateTime1);

        LocalDate localDate1 = LocalDate.of(2014, Month.DECEMBER, 12);
        System.out.println("date3: " + localDate1);

        LocalTime localTime = LocalTime.of(22, 15);
        System.out.println("date4: " +  localTime);

        LocalTime localTime1 = LocalTime.parse("20:15:30");
        System.out.println("date5: " + localTime1);


    }

    /**
     * 时区
     */
    public static void testZoneDateTime(){
        // 获取当前时间日期
        ZonedDateTime date1 = ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
        System.out.println("date1: " + date1);

        ZoneId id = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId: " + id);

        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("当前时区： " + currentZone);

        ZoneId america = ZoneId.of("America/New_York");
        LocalDateTime localtDateAndTime = LocalDateTime.now();
        ZonedDateTime dateAndTimeInNewYork  = ZonedDateTime.of(localtDateAndTime, america);
        System.out.println(dateAndTimeInNewYork);

        ZoneOffset zoneOffset = ZoneOffset.of("+05:30");
        OffsetDateTime date = OffsetDateTime.of(localtDateAndTime, zoneOffset);
        System.out.println("OffsetDateTime: " + date);



        // 上海时间
        ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime shanghaiZonedDateTime = ZonedDateTime.now(shanghaiZoneId);

        // 东京时间
        ZoneId tokyoZoneId = ZoneId.of("Asia/Tokyo");
        ZonedDateTime tokyoZonedDateTime = ZonedDateTime.now(tokyoZoneId);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("上海时间: " + shanghaiZonedDateTime);
        System.out.println("上海时间: " + shanghaiZonedDateTime.format(formatter));
        System.out.println("东京时间: " + tokyoZonedDateTime);
        System.out.println("东京时间: " + tokyoZonedDateTime.format(formatter));
    }
}
